<?php
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Document\Controller;
use \Common\Controller\BaseController;
Class AdvertFactoryController extends BaseController {
    public function _initialize() {
    	parent::_initialize();
        $this->categorys = getcache('category', 'commons');
        $this->modelid = 4;
        $this->models = getcache('model', 'commons');
        $this->userid = (int) cookie('_userid');
        $this->db = D('AdvertLog');
        /* 将页码变量赋值到模板 */
    }

    /* 前台提交信息 */
    public function dosubmit() {
        $uid  = (int) cookie('_userid');
        if ($uid < 1) {
            $this->error('您尚未登陆');
        }
        $model = $this->models[$this->modelid];
        $param = I('param.');
        extract($param);
        if(!$code || !$id) $this->error('参数错误');
        $rs = M($model['tablename'])->getById($id);
        if(!$rs) $this->error('任务不存在');
        $this->db = M($model['tablename'].'_log');
        
        $do_num = (int) $rs['do_num'];
        if ($do_num > 0) {
            $sqlmap = array();
            $sqlmap['ad_id'] = $id;
            $sqlmap['uid'] = $uid;
            $sqlmap['status'] = 1;
            if ($this->db->where($sqlmap)->count()) {
                $this->error('本任务最多可做 '.$do_num.' 次');
            }
        }
        /* 添加会员记录 */
        $log_info = array();
        $log_info['ad_id'] = $id;
        $log_info['uid'] = $uid;
        $log_info['content'] = $code;
        $log_info['status'] = 0;
        $log_info['dateline'] = NOW_TIME;
        $result = $this->db->add($log_info);
        if (!$result) {
            $this->error('信息提交失败，请稍候失败');
        }
        $this->success('信息提交成功，请耐心等待审核');
    }

    /* 前台领取奖励 */
    public function award() {
        $uid  = (int) cookie('_userid');
        if ($uid < 1) {
            $this->error('您尚未登陆');
        }
        $id = I('id', 0, 'intval');
        $rs = $this->db->getById($id);
        if (!$rs) $this->error('参数错误');
        if ($rs['status'] != 1 || $rs['uid'] != $uid) {
            $this->error('没有权限领取');
        }
        $model = $this->models[$this->modelid];
        $document_info = M($model['tablename'])->getById($rs['ad_id']);
        if (!$document_info) {
            $this->error('任务不存在');
        }
        /* 开始颁奖 */
        $result = $this->db->where(array('id' => $id))->save(array('status' => '2', 'updatetime' => NOW_TIME));
        if (!$result) {
            $this->error('领取失败，请稍候重试');
        }
        if ((int) $document_info['do_coin'] > 0) {
            M('Member')->where(array('userid' => $rs['uid']))->setInc("u_coin", $document_info['do_coin']);
            score_change($rs['uid'], '体验广告奖励('.$document_info['title'].')', $document_info['do_coin'], 'u_coin', '1', id_encode($this->modelid, $rs['ad_id']));
        }
        if ((int) $document_info['do_bean'] > 0) {
            M('Member')->where(array('userid' => $rs['uid']))->setInc("u_bean", $document_info['do_bean']);
            score_change($rs['uid'], '体验广告奖励('.$document_info['title'].')', $document_info['do_bean'], 'u_bean', '1', id_encode($this->modelid, $rs['ad_id']));
        }
        $result = array();
        $result['status'] = 1;
        $result['u_coin'] = $document_info['do_coin'];
        $result['u_bean'] = $document_info['do_bean'];
        echo json_encode($result);
    }
}